package com.surenpi.jenkins.client.workflow;

import com.surenpi.jenkins.client.BaseManager;
import com.surenpi.jenkins.client.JenkinsClient;
import com.surenpi.jenkins.client.job.BuildDetail;
import com.surenpi.jenkins.client.job.Jobs;
import com.surenpi.jenkins.client.util.EncodingUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.client.HttpResponseException;

public class Workflows extends BaseManager {
   public WfWithDetails getWfDescribe(String jobName, int buildNo) throws IOException {
      String path = "/";

      try {
         JenkinsClient client = this.getClient();
         WfWithDetails wfWithDetails = (WfWithDetails)this.getClient().get(path + "job/" + EncodingUtils.encode(jobName) + "/" + buildNo + "/wfapi/describe", WfWithDetails.class);
         wfWithDetails.setClient(client);
         this.setBuildingInfo(wfWithDetails, jobName);
         return wfWithDetails;
      } catch (HttpResponseException e) {
         if (e.getStatusCode() == 404) {
            return null;
         } else {
            throw e;
         }
      }
   }

   public Stage getWfNodeDescribe(String jobName, int buildNo, int stageId) throws IOException {
      String path = "/";

      try {
         JenkinsClient client = this.getClient();
         Stage stage = (Stage)client.get(path + "job/" + EncodingUtils.encode(jobName) + "/" + buildNo + "/execution/node/" + stageId + "/wfapi/describe", Stage.class);
         if (null != stage && CollectionUtils.isNotEmpty(stage.getStageFlowNodes())) {
            stage.setClient(client);

            for(StageFlowNodes stageFlowNode : stage.getStageFlowNodes()) {
               int nodeId = stageFlowNode.getId();
               StageFlowNodesLog log = (StageFlowNodesLog)client.get(path + "job/" + EncodingUtils.encode(jobName) + "/" + buildNo + "/execution/node/" + nodeId + "/wfapi/log", StageFlowNodesLog.class);
               log.setClient(client);
               stageFlowNode.setLog(log);
            }
         }

         return stage;
      } catch (HttpResponseException e) {
         if (e.getStatusCode() == 404) {
            return null;
         } else {
            throw e;
         }
      }
   }

   public WfWithDetails last(String jobName) throws IOException {
      String url = "/job/" + EncodingUtils.encode(jobName) + "/lastBuild/wfapi/describe";
      WfWithDetails details = (WfWithDetails)this.getClient().get(url, WfWithDetails.class);
      this.setBuildingInfo(details, jobName);
      return details;
   }

   public void restart(String jobName, int buildNum, String stage) throws IOException {
      String url = "/job/" + EncodingUtils.encode(jobName) + "/" + buildNum + "/restart/restart/";
      Map<String, Object> data = new HashMap();
      data.put("stageName", stage);
      this.getClient().postFormJson(url, data, this.isCrumb());
   }

   private void setBuildingInfo(WfWithDetails details, String jobName) throws IOException {
      String buildId = details.getId();
      Jobs jobs = new Jobs();
      jobs.setClient(this.getClient());
      BuildDetail buildDetail = jobs.getBuildDetails(jobName, Integer.parseInt(buildId));
      boolean building = buildDetail.isBuilding();
      details.setBuilding(building);
   }

   protected String[] getDependencyArray() {
      return new String[]{"pipeline-rest-api"};
   }
}
